Page({
	data: {
		nickName: '',
		avatarUrl: "",
		isLogin: true,
		isMask: true,
		editPhoneing: false,
		phone: '未绑定手机号',
		newPhone: ''
	},
	onLoad() {
		let _this = this
		let user = JSON.parse(wx.getStorageSync('user'))
		if (!user) {
			this.setData({
				isLogin: true,
				isMask: true,
			})
		} else {
			this.setData({
				nickName: user.nickName,
				avatarUrl: user.avatarUrl,
				isLogin: false
			})
			wx.cloud.callFunction({
				name: 'getUser',
				data: {
					nickName: _this.data.nickName,
					avatarUrl: _this.data.avatarUrl,
					phone: _this.data.phone,
				},
				success: function (res) {
					if (res.result.data) {
						if (res.result.data[0] == '未绑定手机号') {

						} else {
							_this.setData({
								nickName: res.result.data[0].nickName,
								avatarUrl: res.result.data[0].avatarUrl,
								phone: res.result.data[0].phone,
							})
						}
					}
				},
				fail(err) {
					wx.showToast({
						title: '数据拉取失败',
						icon: "error"
					})
				}
			})
		}
	},
	login() {
		wx.getUserProfile({
			desc: '用于完善用户资料',
			success: (res) => {
				res.userInfo.phone = '未绑定手机号'
				try {
					wx.setStorageSync('user', JSON.stringify(res.userInfo))
				} catch (e) {}
			},
			fail(err) {
				wx.showToast({
					title: '取消登录',
					icon: 'error'
				})
			}
		})
	},
	getUserInfo() {
		this.setData({
			isMask: true,
			editPhoneing: true
		})
		if (this.data.phone == '未绑定手机号') {

		}
	},
	maskClose() {
		this.setData({
			isMask: false,
			editPhoneing: false,
			newPhone: ''
		})
	},
	refreshPhone() {
		let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
		if (!reg_tel.test(this.data.newPhone)) {
			wx.showToast({
				title: '请输入正确的手机号',
				icon: 'error'
			})
			return
		}
		let _this = this
		wx.cloud.callFunction({
			name: 'updatePhone',
			data: {
				phone: _this.data.newPhone,
			},
			success: function (res) {
				_this.onLoad()
				wx.showToast({
					title: '手机号更新成功',
					icon: "success"
				})
				_this.maskClose()
			},
			fail(err) {
				console.log(err);
				wx.showToast({
					title: '数据拉取失败',
					icon: "error"
				})
			}
		})
	}
})